﻿using System;
using TechTalk.SpecFlow;
using Agility.TestProject.AppDriver;
using NUnit.Framework;

namespace Agility.Testing.Web.Features.StepDefinitions
{
    [Binding]
    public class LogOnSteps
    {
        
        #region Givens

        [Given(@"I registered as a user of Agility")]
        public void GivenIRegisteredAsAUserOfAgility()
        {
            ((AppDriverBase)ScenarioContext.Current["AppDriver"]).Initialize();
        }
        
        [Given(@"the AddIn is installed and activated")]
        public void GivenTheAddInIsInstalledAndActivated()
        {
            ((AgilityExcelDriver)ScenarioContext.Current["AppDriver"]).EnsureAddInIsInstalledAndActivated();
        }

        [Given(@"I have started MS Excel")]
        public void GivenIHaveStartedMSExcel()
        {
            ((AgilityExcelDriver)ScenarioContext.Current["AppDriver"]).StartExcel();
        }

        [Given(@"I was previously logged on as ""(.*)""")]
        public void GivenIWasPreviouslyLoggedOnAs(string p0)
        {
            ((AgilityExcelDriver)ScenarioContext.Current["AppDriver"]).EnsureRoleAssignment(p0);
            ((AgilityExcelDriver)ScenarioContext.Current["AppDriver"]).SimulatePreviousLogOn();
        }

        [Given(@"the keep-me-logged-on feature is enabled")]
        public void GivenTheKeepMeLoggedOnFeatureIsEnabled()
        {
            ((AgilityExcelDriver)ScenarioContext.Current["AppDriver"]).SetKeepMeLoggedOn(true);
        }

        [Given(@"no user settings exist")]
        public void GivenNoUserSettingsExist()
        {
            ((AgilityExcelDriver)ScenarioContext.Current["AppDriver"]).DeleteUserSettings();
        }

        [Given(@"the keep-me-logged-on feature is disabled")]
        public void GivenTheKeep_Me_Logged_OnFeatureIsDisabled()
        {
            ((AgilityExcelDriver)ScenarioContext.Current["AppDriver"]).SetKeepMeLoggedOn(false);
        }

        #endregion

        #region Whens

        [When(@"I navigate to the Agility tab")]
        public void WhenINavigateToTheAgilityTab()
        {
            ((AgilityExcelDriver)ScenarioContext.Current["AppDriver"]).OpenAgilityTab();
        }
        
        [When(@"I press Log On")]
        public void WhenIPressLogOn()
        {
            ((AgilityExcelDriver)ScenarioContext.Current["AppDriver"]).Logon();
        }

        [When(@"I provide username and password in the Settings Dialog")]
        public void WhenIProvideUsernameAndPasswordInTheSettingsDialog()
        {
            ((AgilityExcelDriver)ScenarioContext.Current["AppDriver"]).FillInLoginData();
        }

        [When(@"I press OK")]
        public void WhenIPressOK()
        {
            ((AgilityExcelDriver)ScenarioContext.Current["AppDriver"]).ConfirmSettingsDialog();
        }

        #endregion

        #region Thens

        [Then(@"the Log On button should be disabled")]
        public void ThenTheLogOnButtonShouldBeDisabled()
        {
            Assert.That(((AgilityExcelDriver)ScenarioContext.Current["AppDriver"]).VerifyLogon(), Is.True);
        }

        [Then(@"the Log On button should be enabled")]
        public void ThenTheLogOnButtonShouldBeEnabled()
        {
            Assert.That(((AgilityExcelDriver)ScenarioContext.Current["AppDriver"]).VerifyLogon(), Is.False);
        }

        #endregion
    }
}
